package com.gfxy.modules.index.controller;

import org.apache.poi.xwpf.usermodel.*;
import org.jeecgframework.poi.excel.view.MiniAbstractExcelView;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Controller("TeacherIndexDocView")
public class TeacherIndexDocView extends MiniAbstractExcelView {
    public static final String CONTENT = "content";
    @Value(value = "${jeecg.path.template}")
    private String template;
    public TeacherIndexDocView(){}

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String codedFileName = "事业单位工作人员年度考核登记表.docx";
        File file = new File(template + File.separator + codedFileName);
        InputStream inputStream = new FileInputStream(file);
        XWPFDocument xwpfDocument = new XWPFDocument(inputStream);

        Map<String,String> paramMap = (Map<String, String>) model.get(CONTENT);

        List<XWPFParagraph> paragraphList = xwpfDocument.getParagraphs();
        for (XWPFParagraph xwpfParagraph : paragraphList) {

            List<XWPFRun> list = xwpfParagraph.getRuns();
            for (XWPFRun xwpfRun : list) {

                if(xwpfRun.text().trim().equals("date")){
                    xwpfRun.setText(paramMap.get("currentDate"),0);
                }
            }
        }

        List<XWPFTable> tableList = xwpfDocument.getTables();
        if(tableList.size() > 0) {
            XWPFTable table =  tableList.get(0);
            List<XWPFTableRow> rows = table.getRows();
            XWPFTableRow xwpfTableRow0 = rows.get(0);
            XWPFTableCell cell0_1 = xwpfTableRow0.getCell(1);
            cell0_1.setText(paramMap.get("name"));

            XWPFTableCell cell0_3 = xwpfTableRow0.getCell(3);
            cell0_3.setText(paramMap.get("gender"));

            XWPFTableCell cell0_5 = xwpfTableRow0.getCell(5);
            cell0_5.setText(paramMap.get("birthDay"));


            XWPFTableRow xwpfTableRow1 = rows.get(1);
            XWPFTableCell cell1_1 = xwpfTableRow1.getCell(1);
            cell1_1.setText(paramMap.get("policital-status"));

            XWPFTableCell cell1_3 = xwpfTableRow1.getCell(3);
            cell1_3.setText(paramMap.get("education"));

            XWPFTableCell cell1_5 = xwpfTableRow1.getCell(5);
            cell1_5.setText(paramMap.get("word-time"));


            XWPFTableRow xwpfTableRow2 = rows.get(2);
            XWPFTableCell cell2_1 = xwpfTableRow2.getCell(1);
            cell2_1.setText(paramMap.get("department"));

            XWPFTableRow xwpfTableRow3 = rows.get(3);
            XWPFTableCell cell3_1 = xwpfTableRow3.getCell(1);
            cell3_1.setText(paramMap.get("position-type"));

            XWPFTableCell cell3_3 = xwpfTableRow3.getCell(3);
            cell3_3.setText(paramMap.get("position-name"));

            XWPFTableRow xwpfTableRow5 = rows.get(5);
            XWPFTableCell cell5_0 = xwpfTableRow5.getCell(0);
            cell5_0.setText(paramMap.get("classtime-score"));
            XWPFTableCell cell5_1 = xwpfTableRow5.getCell(1);
            cell5_1.setText(paramMap.get("teaching-score"));
            XWPFTableCell cell5_2 = xwpfTableRow5.getCell(2);
            cell5_2.setText(paramMap.get("honour-score"));
            XWPFTableCell cell5_3 = xwpfTableRow5.getCell(3);
            cell5_3.setText(paramMap.get("research-score"));
            XWPFTableCell cell5_4 = xwpfTableRow5.getCell(4);
            cell5_4.setText(paramMap.get("education-score"));
            XWPFTableCell cell5_5 = xwpfTableRow5.getCell(5);
            cell5_5.setText(paramMap.get("total-score"));
            XWPFTableCell cell5_6 = xwpfTableRow5.getCell(6);
            cell5_6.setText(paramMap.get("ranked-org"));
        }



        if (this.isIE(request)) {
            codedFileName = URLEncoder.encode(codedFileName, "UTF8");
        } else {
            codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
        }


        response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
        ServletOutputStream out = response.getOutputStream();
        xwpfDocument.write(out);
        out.flush();
        out.close();
        inputStream.close();
    }
}
